package org.skydiveapplibrary.util;

import java.io.File;

import org.skydiveapplibrary.R;
import org.skydiveapplibrary.util.DatabaseImportUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class ImportTask extends AsyncTask<ImportSource, Integer, Integer>
{
	// message types
	private static final int SUCCESS = 11;
	private static final int ERROR = 21;
	
	// import file name
	private static final String IMPORT_FILE_NAME = "skydiving_logbook.xml";
	// dropbox folder
	private static final String DROPBOX_FOLDER = "skydiving_logbook";

	private Activity activity;
	private ProgressDialog progressDialog = null;
	
	public ImportTask(Activity activity)
	{
		this.activity = activity;
	}
	
	@Override
	protected void onPreExecute()
	{
		// show dialog
		progressDialog = ProgressDialog.show(activity,
				"",
				activity.getString(R.string.import_export_import_progress_message),
				true, true);
		progressDialog.show();
	}

	@Override
	protected Integer doInBackground(ImportSource... sources)
	{
		int result = SUCCESS;
		
		ImportSource source = sources[0];
		try
		{
			if (source == ImportSource.SDCard)
			{
				result = importFromSDCard();
			}
			else if (source == ImportSource.DropBox)
			{
				result = importFromDropBox();
			}
		}
		catch (Exception ex)
		{
			Log.e(ImportTask.class.getCanonicalName(), ex.getMessage());
			result = ERROR;
		}
		
		return result;
	}
	
	@Override
	protected void onPostExecute(Integer result)
	{
		// dismiss dialog
		progressDialog.dismiss();
		
		// get resource for result
		int resourceId = (result == SUCCESS) ? R.string.import_export_import_success :
			R.string.import_export_import_error;
		
		// show toast
		Toast status = Toast.makeText(activity, resourceId, Toast.LENGTH_SHORT);
		status.show();
	}
	
	private int importFromSDCard()
		throws Exception
	{
		String fileName = Environment.getExternalStorageDirectory() + "/" + IMPORT_FILE_NAME;
		File importFile = new File(fileName);
		if (!importFile.exists())
		{
			return ERROR;
		}
		else
		{
			// do import
			DatabaseImportUtil importUtil = new DatabaseImportUtil(activity);
			importUtil.writeXmlToDB(importFile);

			return SUCCESS;
		}
	}
	
	private int importFromDropBox()
		throws Exception
	{
		// get destination import file
		File importFile = new File(activity.getApplication().getFilesDir(), IMPORT_FILE_NAME);
		// delete if it already exists
		if (importFile.exists())
		{
			importFile.delete();
		}
		
		// create blank file
		if (!importFile.exists())
		{
			importFile.createNewFile(); // otherwise dropbox client will fail silently
		}
		
		// download from dropbox
		DropBoxClient.getInstance().getFile(DROPBOX_FOLDER, IMPORT_FILE_NAME, importFile);
		
		// do import
		DatabaseImportUtil importUtil = new DatabaseImportUtil(activity);
		importUtil.writeXmlToDB(importFile);

		return SUCCESS;
	}
}